<?php
/**
 * Created by PhpStorm.
 * User: 86158
 * Date: 2021/6/29
 * Time: 17:16
 */

namespace App\Models\news;


use App\Exceptions\api\BaseException;
use App\Models\base\traits\ImgTrait;
use App\Models\Model;
use Encore\Admin\Traits\AdminBuilder;
use Encore\Admin\Traits\ModelTree;

class NewsCate extends Model
{
    use ImgTrait , ModelTree, AdminBuilder;

    public function __construct(array $attributes = [])
    {
        parent::__construct($attributes);
        $this->setParentColumn('pid');
        $this->setOrderColumn('created_at');
        $this->setTitleColumn('name');
    }

    public function delete()
    {
        $find = self::findOrFail($this->getKey());
        $find->status = 0;
        $count = News::where('cate_id',$this->getKey())->count();
        if ($count > 0) {
            throw new BaseException('该分类下还存在资讯文章,暂不能删除');
        }
        if ($find->pid == 0) {
            throw new BaseException('一级分类不能删除');
        }
        return $find->save();
    }
}